#!/usr/bin/php
<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

date_default_timezone_set('UTC'); 

define('CURRENT_DIR',dirname(__FILE__));
$last_slash = strrpos(dirname(__FILE__), "/");
if ($last_slash<=0) {
	$last_slash = strrpos(dirname(__FILE__), "\\");
}
define('SYSDIR',substr(CURRENT_DIR, 0, $last_slash));

$debugenabled=false;
$maildebugenabled=false;
$dbdebugenabled=false;
//error_reporting(0);
ini_set('display_errors', true);

include_once(SYSDIR."/configs/dbconf.php");
include_once(CURRENT_DIR."/func_stats.php");

require_once(SYSDIR."/class/dbmanager.class.php");
require_once(SYSDIR."/class/loader.class.php");
require_once(SYSDIR."/includes/util.func.php");

require_once(SYSDIR."/class/user.class.php");
require_once(SYSDIR."/class/task.class.php");
require_once(SYSDIR."/class/def.class.php");

$db = new dbmanager(DB_HOST,DB_USER,DB_PASS);

$db->connect(DB_NAME);
$loader = new loader($db);

/* LOAD DEFINITIONS */
$defs = simplexml_load_file(SYSDIR."/configs/definitions.xml");
foreach($defs->definition as $def) {
   eval("define('{$def->name}', '{$def->value}');");
}

$defs = simplexml_load_file(SYSDIR."/configs/tables.xml");
foreach($defs->table as $def) {
   eval("define('{$def->name}', '{$def->value}');");
}

$def = new def;
$list = $loader->loadList($def);

foreach($list as $obj) {
   eval("define('{$obj->id}', '{$obj->value}');");
}

if (ENABLE_CUSTOM_SMTP == 'true') {
	debug("Using custom SMTP");
	ini_set("smtp",SMTP_HOST);
	ini_set("smtp_port",SMTP_PORT);
}

/* END */

function notify($user,$task,$type) {
	include(SYSDIR.'/lang/mail_lang_'.$user->lang.'.inc');
	$ar = array(date('Y-m-d',time()),$MAIL[$type],"\"".$task->task."\"");
	$text = createMessage($MAIL['tasknotification'],$ar);
	$subject = $text;
	$headers = 'From: '.SENDER_ADDRESS;
	$r = mail($user->mail,$subject, $text,$headers);
}

$task = new task;
$startlist = $loader->loadListBy($task,$task->listBeginTasks());
$endlist = $loader->loadListBy($task,$task->listEndTasks());

echo "Stating tasks ".count($startlist)."\n";
echo "Ending tasks ".count($endlist)."\n";

$u = new user;
$u->status=STATUS_ACTIVE;
$userlist = $loader->loadListBy($u,$u->listByStatus());

$notu=0;
foreach ($startlist as $tk) {
	if ($tk->teamid=="0") {
		$ul=$userlist;
	} else {
		$u = new user;
		$u->status=STATUS_ACTIVE;
		$u->teamid=$tk->teamid;
		$ul = $loader->loadListBy($u,$u->listByFilters());
	}
	foreach ($ul as $user) {
		if ($user->notifyTask()===true) {
			notify($user,$tk,"taskbegins");
			$notu+=1;
		}
	}
	$loader->execute($tk->setStatus("1"));
}

foreach ($endlist as $tk) {
	if ($tk->teamid=="0") {
		$ul=$userlist;
	} else {
		$u = new user;
		$u->status=STATUS_ACTIVE;
		$u->teamid=$tk->teamid;
		$ul = $loader->loadListBy($u,$u->listByFilters());
	}
	foreach ($ul as $user) {
		if ($user->notifyTask()===true) {
			notify($user,$tk,"taskfinish");
			$notu+=1;
		}
	}
	$loader->execute($tk->setStatus("2"));
}

echo "Notified users ".$notu."\n";
$db->close();

?>